package service

import (
	"context"
	"krm_pro/config"

	"github.com/gin-gonic/gin"
	corev1 "k8s.io/api/core/v1"
	metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
)

type serviceInfo struct {
	ServiceName      string `json:"name"`
	ServiceNamespace string `json:"servicenamespace"`
	Service          *corev1.Service
}

var clientsetService = config.ClientSet.CoreV1().Services

func ServiceList(r *gin.Context) {
	serviceinfo := serviceInfo{}
	if err := r.ShouldBindJSON(&serviceinfo); err != nil {
		r.JSON(200, gin.H{
			"status":  400,
			"message": "should bind false",
		})
		return
	}
	s := serviceinfo.list(serviceinfo.ServiceNamespace)
	r.JSON(200, gin.H{
		"status": 200,
		"data":   s.Items,
	})
}

func ServiceGet(r *gin.Context) {
	serviceinfo := serviceInfo{}
	if err := r.ShouldBindJSON(&serviceinfo); err != nil {
		r.JSON(200, gin.H{
			"status":  400,
			"message": "should bind false",
		})
		return
	}
	s := serviceinfo.get(serviceinfo.ServiceNamespace, serviceinfo.ServiceName)
	r.JSON(200, gin.H{
		"status": 200,
		"data":   s,
	})
}

func ServiceCreate(r *gin.Context) {
	serviceinfo := serviceInfo{}
	if err := r.ShouldBindJSON(&serviceinfo); err != nil {
		r.JSON(200, gin.H{
			"status":  400,
			"message": "should bind false",
		})
		return
	}
	s := serviceinfo.create(serviceinfo.ServiceNamespace, serviceinfo.Service)
	r.JSON(200, gin.H{
		"status": 200,
		"data":   s,
	})
}

func ServiceUpdate(r *gin.Context) {
	serviceinfo := serviceInfo{}
	if err := r.ShouldBindJSON(&serviceinfo); err != nil {
		r.JSON(200, gin.H{
			"status":  400,
			"message": "should bind false",
		})
		return
	}
	s := serviceinfo.update(serviceinfo.ServiceNamespace, serviceinfo.Service)
	r.JSON(200, gin.H{
		"status": 200,
		"data":   s,
	})
}

func ServiceDelete(r *gin.Context) {
	serviceinfo := serviceInfo{}
	if err := r.ShouldBindJSON(&serviceinfo); err != nil {
		r.JSON(200, gin.H{
			"status":  400,
			"message": "should bind false",
		})
		return
	}
	serviceinfo.delete(serviceinfo.ServiceNamespace, serviceinfo.ServiceName)
	r.JSON(200, gin.H{
		"status":  200,
		"message": "service delete completed",
	})
}
func (s *serviceInfo) list(namespace string) *corev1.ServiceList {
	service, err := clientsetService(namespace).List(context.TODO(), metav1.ListOptions{})
	if err != nil {
		panic(err.Error())
	}
	return service
}

func (s *serviceInfo) get(namespace string, name string) *corev1.Service {
	service, err := clientsetService(namespace).Get(context.TODO(), name, metav1.GetOptions{})
	if err != nil {
		panic(err.Error())
	}
	return service
}

func (s *serviceInfo) create(namespace string, service *corev1.Service) *corev1.Service {
	sec, err := clientsetService(namespace).Create(context.TODO(), service, metav1.CreateOptions{})
	if err != nil {
		panic(err.Error())
	}
	return sec
}

func (s *serviceInfo) update(namespace string, service *corev1.Service) *corev1.Service {
	sec, err := clientsetService(namespace).Update(context.TODO(), service, metav1.UpdateOptions{})
	if err != nil {
		panic(err.Error())
	}
	return sec
}

func (s *serviceInfo) delete(namespace string, name string) {
	err := clientsetService(namespace).Delete(context.TODO(), name, metav1.DeleteOptions{})
	if err != nil {
		panic(err.Error())
	}
}
